package com.chezai.client.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.chezai.client.bean.ClassRecord;
import com.chezai.client.bean.CoachLoginRecord;
import com.chezai.client.bean.UserInfo;

import com.chezai.client.bean.StudentStudyRecord;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "STUDENT_STUDY_RECORD".
*/
public class StudentStudyRecordDao extends AbstractDao<StudentStudyRecord, Long> {

    public static final String TABLENAME = "STUDENT_STUDY_RECORD";

    /**
     * Properties of entity StudentStudyRecord.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property _ID = new Property(0, Long.class, "_ID", true, "_id");
        public final static Property StudentId = new Property(1, Long.class, "studentId", false, "STUDENT_ID");
        public final static Property CoachLoginRecordId = new Property(2, Long.class, "coachLoginRecordId", false, "COACH_LOGIN_RECORD_ID");
        public final static Property ClassRecordId = new Property(3, Long.class, "classRecordId", false, "CLASS_RECORD_ID");
        public final static Property CreateDay = new Property(4, String.class, "createDay", false, "CREATE_DAY");
        public final static Property SiginInTime = new Property(5, Long.class, "siginInTime", false, "SIGIN_IN_TIME");
        public final static Property SiginOutTime = new Property(6, Long.class, "siginOutTime", false, "SIGIN_OUT_TIME");
        public final static Property UpdateTime = new Property(7, Long.class, "updateTime", false, "UPDATE_TIME");
    }

    private DaoSession daoSession;


    public StudentStudyRecordDao(DaoConfig config) {
        super(config);
    }
    
    public StudentStudyRecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"STUDENT_STUDY_RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: _ID
                "\"STUDENT_ID\" INTEGER," + // 1: studentId
                "\"COACH_LOGIN_RECORD_ID\" INTEGER," + // 2: coachLoginRecordId
                "\"CLASS_RECORD_ID\" INTEGER," + // 3: classRecordId
                "\"CREATE_DAY\" TEXT," + // 4: createDay
                "\"SIGIN_IN_TIME\" INTEGER," + // 5: siginInTime
                "\"SIGIN_OUT_TIME\" INTEGER," + // 6: siginOutTime
                "\"UPDATE_TIME\" INTEGER);"); // 7: updateTime
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"STUDENT_STUDY_RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, StudentStudyRecord entity) {
        stmt.clearBindings();
 
        Long _ID = entity.get_ID();
        if (_ID != null) {
            stmt.bindLong(1, _ID);
        }
 
        Long studentId = entity.getStudentId();
        if (studentId != null) {
            stmt.bindLong(2, studentId);
        }
 
        Long coachLoginRecordId = entity.getCoachLoginRecordId();
        if (coachLoginRecordId != null) {
            stmt.bindLong(3, coachLoginRecordId);
        }
 
        Long classRecordId = entity.getClassRecordId();
        if (classRecordId != null) {
            stmt.bindLong(4, classRecordId);
        }
 
        String createDay = entity.getCreateDay();
        if (createDay != null) {
            stmt.bindString(5, createDay);
        }
 
        Long siginInTime = entity.getSiginInTime();
        if (siginInTime != null) {
            stmt.bindLong(6, siginInTime);
        }
 
        Long siginOutTime = entity.getSiginOutTime();
        if (siginOutTime != null) {
            stmt.bindLong(7, siginOutTime);
        }
 
        Long updateTime = entity.getUpdateTime();
        if (updateTime != null) {
            stmt.bindLong(8, updateTime);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, StudentStudyRecord entity) {
        stmt.clearBindings();
 
        Long _ID = entity.get_ID();
        if (_ID != null) {
            stmt.bindLong(1, _ID);
        }
 
        Long studentId = entity.getStudentId();
        if (studentId != null) {
            stmt.bindLong(2, studentId);
        }
 
        Long coachLoginRecordId = entity.getCoachLoginRecordId();
        if (coachLoginRecordId != null) {
            stmt.bindLong(3, coachLoginRecordId);
        }
 
        Long classRecordId = entity.getClassRecordId();
        if (classRecordId != null) {
            stmt.bindLong(4, classRecordId);
        }
 
        String createDay = entity.getCreateDay();
        if (createDay != null) {
            stmt.bindString(5, createDay);
        }
 
        Long siginInTime = entity.getSiginInTime();
        if (siginInTime != null) {
            stmt.bindLong(6, siginInTime);
        }
 
        Long siginOutTime = entity.getSiginOutTime();
        if (siginOutTime != null) {
            stmt.bindLong(7, siginOutTime);
        }
 
        Long updateTime = entity.getUpdateTime();
        if (updateTime != null) {
            stmt.bindLong(8, updateTime);
        }
    }

    @Override
    protected final void attachEntity(StudentStudyRecord entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public StudentStudyRecord readEntity(Cursor cursor, int offset) {
        StudentStudyRecord entity = new StudentStudyRecord( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // _ID
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // studentId
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // coachLoginRecordId
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // classRecordId
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // createDay
            cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5), // siginInTime
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // siginOutTime
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7) // updateTime
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, StudentStudyRecord entity, int offset) {
        entity.set_ID(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setStudentId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setCoachLoginRecordId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setClassRecordId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setCreateDay(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setSiginInTime(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5));
        entity.setSiginOutTime(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setUpdateTime(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(StudentStudyRecord entity, long rowId) {
        entity.set_ID(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(StudentStudyRecord entity) {
        if(entity != null) {
            return entity.get_ID();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(StudentStudyRecord entity) {
        return entity.get_ID() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserInfoDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getClassRecordDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getCoachLoginRecordDao().getAllColumns());
            builder.append(" FROM STUDENT_STUDY_RECORD T");
            builder.append(" LEFT JOIN USER_INFO T0 ON T.\"STUDENT_ID\"=T0.\"_id\"");
            builder.append(" LEFT JOIN CLASS_RECORD T1 ON T.\"CLASS_RECORD_ID\"=T1.\"_id\"");
            builder.append(" LEFT JOIN COACH_LOGIN_RECORD T2 ON T.\"COACH_LOGIN_RECORD_ID\"=T2.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected StudentStudyRecord loadCurrentDeep(Cursor cursor, boolean lock) {
        StudentStudyRecord entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        UserInfo stuInfo = loadCurrentOther(daoSession.getUserInfoDao(), cursor, offset);
        entity.setStuInfo(stuInfo);
        offset += daoSession.getUserInfoDao().getAllColumns().length;

        ClassRecord classRecord = loadCurrentOther(daoSession.getClassRecordDao(), cursor, offset);
        entity.setClassRecord(classRecord);
        offset += daoSession.getClassRecordDao().getAllColumns().length;

        CoachLoginRecord coachLoginRecord = loadCurrentOther(daoSession.getCoachLoginRecordDao(), cursor, offset);
        entity.setCoachLoginRecord(coachLoginRecord);

        return entity;    
    }

    public StudentStudyRecord loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<StudentStudyRecord> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<StudentStudyRecord> list = new ArrayList<StudentStudyRecord>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<StudentStudyRecord> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<StudentStudyRecord> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
